package com.lbs.tdrive.activity;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lbs.tdrive.comon.HttpASync;
import com.lbs.tdrive.comon.HttpASync.OnRequest;
import com.lbs.tdrive.constant.AppConstant;
import com.lbs.tdrive.data.AlertData;
import com.lbs.tdrive.activity.Alerts;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbs.tdrive.R;

import com.lbs.view.adapter.AlertAdapter;

public class Alerts extends Activity implements OnClickListener, OnRequest {
	private ListView alertList;
	private AlertAdapter adapter;
	private TextView alerttext;
	private ImageView deletealert;
	private Context ctx;
	public static final int DELETE_ALERT=1;
	private Handler handler=new Handler(){
		

		public void handleMessage(android.os.Message msg) {
			if(msg.what==Alerts.DELETE_ALERT) {
				
				Toast.makeText(ctx, "Delete", Toast.LENGTH_SHORT).show();
				
			}
		};
	};
	  
	     
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        ctx=this;
	        setContentView(R.layout.alerts);
	        alertList=(ListView)findViewById(R.id.listView1);
	        
	 }

public void onBackPressed() {
	// TODO Auto-generated method stub
	finish();
}
@Override
public void onClick(View v) {
	// TODO Auto-generated method stub
	
}
@Override
public void onResponseReceived(String response) {
	Gson gson = new Gson();
	
	final List<AlertData> alerteListData = gson.fromJson(response,new TypeToken<List<AlertData>>(){}.getType());
	for (AlertData alert : alerteListData) {
		System.out.println("Alert : "+alert.getAlertName());
	}
	runOnUiThread(new Runnable() {
		
		@Override
		public void run() {
			adapter=new AlertAdapter(ctx,handler,alerteListData);
			alertList.setAdapter(adapter);
		}
	});
	
}

@Override
protected void onResume() {
	// TODO Auto-generated method stub
	super.onResume();
	SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME,0);
	String uName = pref.getString("username", null);
	HttpASync async = new HttpASync();
	String url=AppConstant.ALERT+"?username="+uName+"&lat=8.521056&long=76.941712";
	async.setOnRequestListener(this);
	async.execute(url);
}

}
